package com.xinghen.common.core.support;

import com.xinghen.common.base.GlobalConstant;
import com.xinghen.common.base.enums.ErrorType;
import com.xinghen.common.base.exception.BusinessException;
import com.xinghen.common.base.model.dto.LoginUser;
import com.xinghen.common.base.model.response.RestResult;
import com.xinghen.common.util.ThreadContextHolder;
import com.xinghen.common.zookeeper.generator.impl.IncrementIdGenerator;
import com.xinghen.common.zookeeper.generator.impl.UniqueIdGenerator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * @author xinghen
 * @ClassName: BaseController
 * @Package: com.xinghen.common.core.support
 * @Description Controller基类
 * @create 2018-04-17 11:17
 */
public class BaseController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 获取当前登录用户
     *
     * @return
     */
    protected LoginUser currentLoginUser() {
        LoginUser user = (LoginUser) ThreadContextHolder.get(GlobalConstant.Sys.CURRENT_LOGIN_USER);
        if (Objects.isNull(user)) {
            throw new BusinessException(ErrorType.UAC10011041);
        }
        return user;
    }

    /**
     * 处理返回结果
     *
     * @param result
     * @param <T>
     * @return
     */
    protected <T> RestResult<T> handleResult(T result) {
        if (isValid(result)) {
            return RestResult.ok(result);
        } else {
            return RestResult.error();
        }
    }

    /**
     * 处理返回结果
     *
     * @param result
     * @param errorMessage
     * @param <T>
     * @return
     */
    protected <T> RestResult<T> handleResult(T result, String errorMessage) {
        if (isValid(result)) {
            return RestResult.ok(result);
        } else {
            return RestResult.error(errorMessage);
        }
    }

    /**
     * 判断返回结果是否合法
     *
     * @param result
     * @param <T>
     * @return
     */
    private <T> boolean isValid(T result) {
        boolean flag;
        if (result instanceof Integer) {
            flag = (Integer) result > 0;
        } else if (result instanceof Boolean) {
            flag = (Boolean) result;
        } else if (result instanceof String) {
            flag = StringUtils.isNotBlank((String) result);
        } else if (result instanceof Collection) {
            flag = CollectionUtils.isNotEmpty((Collection) result);
        } else if (result instanceof Map) {
            flag = MapUtils.isNotEmpty((Map) result);
        } else {
            flag = Objects.nonNull(result);
        }
        return flag;
    }

    /**
     * 生成ID(必须启动zookeeper，并配置zookeeper参数)
     *
     * @return
     */
    protected long generateId() {
        return UniqueIdGenerator.getInstance(IncrementIdGenerator.getServiceId()).nextId();
    }

}
